package com.rfid.spec.pc301;
import com.rfid.api.GFunction;
import com.rfid.reader.DriverPC301;
import com.rfid.spec.L0_ISO15693_PICC;
import com.rfid.spec.L0_ISO15693_PICC_HANDLE;
import com.rfid.spec.L0_PICC;
import com.rfid.spec.L0_PICC_HANDLE;
import com.rfid.spec.SpecTagAccess;
import com.rfid.spec.tagaccess.SpecEASCheck;
import com.rfid.spec.tagaccess.SpecGetSysInfor;
import com.rfid.spec.tagaccess.SpecLockMultipleBlocks;
import com.rfid.spec.tagaccess.SpecReadMultipleBlocks;
import com.rfid.spec.tagaccess.SpecWriteAFI;
import com.rfid.spec.tagaccess.SpecWriteDSFID;
import com.rfid.spec.tagaccess.SpecWriteMultipleBlocks;
import com.rfid.transport.BufferPack;
import com.rfid.transport.NdkSupport;
public class AN_PC301_ISO15693PICCAccess {
    public static int TagAccess(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        int iret = -3;
        if (tagAccess.m_AccessCode == 0L) {
            L0_PICC picc = (L0_PICC) h.m_picc;
            AN_PC301_APL_ISO15693 apl = (AN_PC301_APL_ISO15693) picc.m_apl;
            iret = apl.CmdReset();
        } else if (tagAccess.m_AccessCode == 7L) iret = CmdGetSysinfo(h, tagAccess);
        else if (tagAccess.m_AccessCode == 1L) iret = ReadMultipleBlocksUnlimited(h, tagAccess);
        else if (tagAccess.m_AccessCode == 2L) iret = CmdWriteMultipleBlksUnlimited(h, tagAccess);
        else if (tagAccess.m_AccessCode == 9L) iret = CmdLockMultipleBlocksUnlimited(h, tagAccess);
        else if (tagAccess.m_AccessCode == 5L) iret = CmdWriteDSFID(h, tagAccess);
        else if (tagAccess.m_AccessCode == 6L) iret = CmdLockDSFID(h, tagAccess);
        else if (tagAccess.m_AccessCode == 3L) iret = CmdWriteAFI(h, tagAccess);
        else if (tagAccess.m_AccessCode == 4L) iret = CmdLockAFI(h, tagAccess);
        else if (tagAccess.m_AccessCode == 160L) iret = CmdEnableEAS(h, tagAccess);
        else if (tagAccess.m_AccessCode == 161L) iret = CmdDisableEAS(h, tagAccess);
        else if (tagAccess.m_AccessCode == 162L) iret = CmdLockEAS(h, tagAccess);
        else if (tagAccess.m_AccessCode == 166L) iret = CmdEASCheck(h, tagAccess);
        return iret;
    }

    private static int CmdGetSysinfo(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        SpecGetSysInfor pAccess = (SpecGetSysInfor) tagAccess;
        if (h == null || tagAccess == null) return -3;
        int iret = -1;
        pReader.m_syncLock.lock();
        pReader.pbSnd.flush();
        pReader.pbRecv.flush();
        char cmd =11262;
        pReader.pbSnd.WriteData(cmd);
        pReader.pbSnd.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) pReader.pbSnd.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, pReader.pbSnd, pReader.pbRecv, 500L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (pReader.pbRecv.readable_length() < 1) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        byte option_flag = pReader.pbRecv.ReadData();
        if (option_flag != 15) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (pReader.pbRecv.readable_length() < 13) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        pReader.pbRecv.ReadData(pAccess.m_respUID, 8);
        GFunction.BytesReverse(pAccess.m_respUID, pAccess.m_respUID.length);
        pAccess.m_respDSFID = pReader.pbRecv.ReadData();
        pAccess.m_respAFI = pReader.pbRecv.ReadData();
        pAccess.m_respTotalBlockNum = pReader.pbRecv.ReadData();
        pAccess.m_respTotalBlockNum++;
        pAccess.m_respSizeOfBlock = (byte) (pReader.pbRecv.ReadData() + 1);
        pAccess.m_respICRef = pReader.pbRecv.ReadData();
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int ReadMultipleBlocksUnlimited32B(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        Byte numRead = new Byte((byte) 0);
        byte[] respuid = new byte[8];
        BufferPack datablocks = new BufferPack(1024);
        SpecReadMultipleBlocks pAccess = (SpecReadMultipleBlocks) tagAccess;
        if (h == null || tagAccess == null) return -3;
        byte blkno = (byte) (pAccess.m_reqStartAddr & 0xFF);
        byte num_of_blks = (byte) (pAccess.m_reqNumBlkToRead & 0xFF);
        boolean read_sec = pAccess.m_reqReadSecSta;
        byte totalNumRead = 0;
        while (num_of_blks > 0) {
            byte numToRead = num_of_blks;
            if (numToRead > 32) numToRead = 32;
            numRead = Byte.valueOf((byte) 0);
            int iret = CmdReadMultiBlks2(h, read_sec, blkno, numToRead, numRead, datablocks, respuid);
            if (iret != 0) return iret;
            blkno = (byte) (blkno + numRead.byteValue());
            num_of_blks = (byte) (num_of_blks - numRead.byteValue());
            totalNumRead = (byte) (totalNumRead + numRead.byteValue());
            if (numRead.byteValue() < numToRead) break;
        }
        pAccess.m_respNumRead = totalNumRead;
        pAccess.m_respBlocksData.WriteData(datablocks);
        return 0;
    }

    private static int CmdReadMultiBlks2(L0_PICC_HANDLE h, boolean read_sec, byte blkno, byte num_of_blks, Byte numBlksRead, BufferPack dataBlock, byte[] respUID) {
        long blkNoTmp = blkno;
        long num_of_blksTmp = num_of_blks;
        if (blkNoTmp < 0L) blkNoTmp += 256L;
        if (num_of_blksTmp < 0L) num_of_blksTmp += 256L;
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        pReader.pbSnd.flush();
        pReader.pbRecv.flush();
        long tagCapacity = ((L0_ISO15693_PICC) h.m_picc).GetBlocksCapacity();
        if (blkNoTmp >= tagCapacity) {
            iret = -3;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (blkNoTmp + num_of_blksTmp > tagCapacity)
            num_of_blks = (byte) (int) (tagCapacity - blkNoTmp & 0xFFL);
        char cmd =9214;
        pReader.pbSnd.WriteData(cmd);
        pReader.pbSnd.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) pReader.pbSnd.WriteData(handle.m_uid, 0, 8);
        pReader.pbSnd.WriteData(blkno);
        pReader.pbSnd.WriteData((byte) (num_of_blks - 1));
        iret = pReader.transceive(cmd, pReader.pbSnd, pReader.pbRecv, 2000L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (pReader.pbRecv.readable_length() < 1) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        byte DB_SIZE = pReader.pbRecv.ReadData();
        DB_SIZE = (byte) (DB_SIZE + 1);
        DB_SIZE = (byte) (DB_SIZE + 1);
        byte DB_N = (byte) (pReader.pbRecv.readable_length() / DB_SIZE);
        int blocksSize = DB_SIZE * DB_N;
        if (read_sec) dataBlock.WriteData(pReader.pbRecv.GetReadtableBuffer(), 0, blocksSize);
        else {
            byte[] block = new byte[16];
            for (int i = 0; i < DB_N; i++) {
                pReader.pbRecv.ReadData(block, DB_SIZE);
                dataBlock.WriteData(block, 1, DB_SIZE - 1);
            }
        }
        NdkSupport.SetByteValue(numBlksRead, DB_N);
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int ReadMultipleBlocksUnlimited8B(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        Byte numRead = new Byte((byte) 0);
        byte[] respuid = new byte[8];
        BufferPack datablocks = new BufferPack(1024);
        SpecReadMultipleBlocks pAccess = (SpecReadMultipleBlocks) tagAccess;
        if (h == null || tagAccess == null) return -3;
        byte blkno = (byte) (pAccess.m_reqStartAddr & 0xFF);
        byte num_of_blks = (byte) (pAccess.m_reqNumBlkToRead & 0xFF);
        boolean read_sec = pAccess.m_reqReadSecSta;
        byte totalNumRead = 0;
        while (num_of_blks > 0) {
            byte numToRead = num_of_blks;
            if (numToRead > 8) numToRead = 8;
            numRead = Byte.valueOf((byte) 0);
            int iret = CmdReadMultiBlks(h, read_sec, blkno, numToRead, numRead, datablocks, respuid);
            if (iret != 0) return iret;
            blkno = (byte) (blkno + numRead.byteValue());
            num_of_blks = (byte) (num_of_blks - numRead.byteValue());
            totalNumRead = (byte) (totalNumRead + numRead.byteValue());
            if (numRead.byteValue() < numToRead) break;
        }
        pAccess.m_respNumRead = totalNumRead;
        pAccess.m_respBlocksData.WriteData(datablocks);
        return 0;
    }

    private static int ReadMultipleBlocksUnlimited(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        int iret;
        DriverPC301 pR = (DriverPC301) h.m_reader;
        if ((pR.m_productType == 6881793L || pR.m_productType == 6816257L) && pR.m_firmVer >= 4356L)
            iret = ReadMultipleBlocksUnlimited32B(h, tagAccess);
        else
            iret = ReadMultipleBlocksUnlimited8B(h, tagAccess);
        return iret;
    }

    private static int CmdReadMultiBlks(L0_PICC_HANDLE h, boolean read_sec, byte blkno, byte num_of_blks, Byte numBlksRead, BufferPack dataBlock, byte[] respUID) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        long blknoTmp = blkno;
        if (blknoTmp < 0L) blknoTmp += 256L;
        long tagCapacity = ((L0_ISO15693_PICC) h.m_picc).GetBlocksCapacity();
        long tagBlkSize = ((L0_ISO15693_PICC) h.m_picc).GetBlockSize();
        if (blknoTmp >= tagCapacity) {
            iret = -3;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (tagBlkSize < 0L) tagBlkSize += 256L;
        if (num_of_blks < 0) if ((blkno + num_of_blks + 256) > tagCapacity)
            num_of_blks = (byte) (int) (tagCapacity - blkno);
        else if ((blkno + num_of_blks) > tagCapacity)
            num_of_blks = (byte) (int) (tagCapacity - blkno);
        char cmd =9214;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        snd_packet.WriteData(blkno);
        snd_packet.WriteData((byte) (num_of_blks - 1));
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 1000L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (recv_packet.readable_length() < 10) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        byte DB_SIZE = 0, DB_N = 0;
        recv_packet.ReadData(respUID, 8);
        DB_SIZE = recv_packet.ReadData();
        DB_SIZE = (byte) (DB_SIZE + 1);
        DB_N = recv_packet.ReadData();
        DB_N = (byte) (DB_N + 1);
        int blocksSize = (int) ((tagBlkSize + 1L) * DB_N);
        if (recv_packet.readable_length() < blocksSize) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (read_sec) dataBlock.WriteData(recv_packet.GetReadtableBuffer(), 0, blocksSize);
        else {
            byte[] block = new byte[5];
            for (int i = 0; i < DB_N; i++) {
                recv_packet.ReadData(block, 5);
                dataBlock.WriteData(block, 1, 4);
            }
        }
        NdkSupport.SetByteValue(numBlksRead, DB_N);
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int CmdWriteMultipleBlksUnlimited(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        SpecWriteMultipleBlocks pAccess = (SpecWriteMultipleBlocks) tagAccess;
        if (h == null || tagAccess == null) return -3;
        byte blkno = (byte) (pAccess.m_reqStartBlock & 0xFF);
        byte blkNum = (byte) (pAccess.m_reqNumOfBlockToWrite & 0xFF);
        byte[] pdata = pAccess.m_reqBlocksData.GetBuffer();
        int dataSize = pAccess.m_reqBlocksData.getBufferLen();
        int idx = 0;
        while (blkNum > 0 && dataSize > 0) {
            byte blksToWrite = blkNum;
            if (blksToWrite > 8) blksToWrite = 8;
            byte bytesWritten = 0, blocksWritten = bytesWritten;
            int iret = CmdWriteMultipleBlks(h, blkno, blksToWrite, pdata, idx, dataSize, Byte.valueOf(blocksWritten), Byte.valueOf(bytesWritten));
            if (iret != 0) return iret;
            if (blocksWritten < blksToWrite) break;
            blkno = (byte) (blkno + blksToWrite);
            blkNum = (byte) (blkNum - blksToWrite);
            idx += bytesWritten;
            dataSize -= bytesWritten;
        }
        return 0;
    }

    private static int CmdWriteMultipleBlks(L0_PICC_HANDLE h, byte blkno, byte numBlks, byte[] blkdata, int byteOffset, int nSize, Byte blksWritten, Byte bytesWritten) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        byte[] eightBlocks = new byte[64];
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        byte tagCapacity = (byte) (int) ((L0_ISO15693_PICC) h.m_picc).GetBlocksCapacity();
        byte tagBlkSize = (byte) (int) ((L0_ISO15693_PICC) h.m_picc).GetBlockSize();
        if (blkno >= tagCapacity) {
            iret = -3;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (blkno + numBlks > tagCapacity) numBlks = (byte) (tagCapacity - blkno);
        if (numBlks > 8) numBlks = 8;
        int nCopy = numBlks * tagBlkSize;
        if (nCopy > nSize) nCopy = nSize;
        System.arraycopy(blkdata, byteOffset, eightBlocks, 0, nCopy);
        char cmd =9470;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        snd_packet.WriteData(blkno);
        snd_packet.WriteData((byte) (numBlks - 1));
        snd_packet.WriteData(eightBlocks, 0, tagBlkSize * numBlks);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        NdkSupport.SetByteValue(blksWritten, numBlks);
        NdkSupport.SetByteValue(bytesWritten, (byte) nCopy);
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int CmdLockMultipleBlocks(L0_PICC_HANDLE h, byte blkno, byte blknum, Byte blkLocked) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        byte tagCapacity = (byte) (int) ((L0_ISO15693_PICC) h.m_picc).GetBlocksCapacity();
        if (blkno >= tagCapacity) {
            iret = -3;
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (blkno + blknum > tagCapacity) blknum = (byte) (tagCapacity - blkno);
        if (blknum > 8) blknum = 8;
        char cmd =8958;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        snd_packet.WriteData(blkno);
        snd_packet.WriteData((byte) (blknum - 1));
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (blkLocked != null) NdkSupport.SetByteValue(blkLocked, blknum);
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int CmdLockMultipleBlocksUnlimited(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        SpecLockMultipleBlocks pAccess = (SpecLockMultipleBlocks) tagAccess;
        if (h == null || tagAccess == null) return -3;
        byte blkno = (byte) pAccess.m_reqStartBlock;
        byte blknum = (byte) pAccess.m_reqNumOfBlock;
        Byte numLocked = new Byte((byte) 0);
        while (blknum > 0) {
            byte blkToLock = blknum;
            if (blkToLock > 8) blkToLock = 8;
            numLocked = Byte.valueOf((byte) 0);
            int iret = CmdLockMultipleBlocks(h, blkno, blkToLock, numLocked);
            if (iret != 0) return iret;
            if (numLocked.byteValue() < blkToLock) break;
            blkno = (byte) (blkno + numLocked.byteValue());
            blknum = (byte) (blknum - numLocked.byteValue());
        }
        return 0;
    }

    private static int CmdWriteDSFID(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        SpecWriteDSFID pAccess = (SpecWriteDSFID) tagAccess;
        if (h == null || tagAccess == null) return -3;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        byte dsfid = pAccess.m_DSFID;
        char cmd=10750;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        snd_packet.WriteData(dsfid);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }

    private static int CmdLockDSFID(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd=11006;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }

    private static int CmdWriteAFI(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        SpecWriteAFI pAccess = (SpecWriteAFI) tagAccess;
        if (h == null || tagAccess == null) return -3;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        byte afi = pAccess.m_AFI;
        char cmd =10238;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        snd_packet.WriteData(afi);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }

    private static int CmdLockAFI(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd =10494;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }

    private static int CmdEnableEAS(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd=41709;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData((byte) 4);
        snd_packet.WriteData((byte) 1);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        if (iret != 0) return iret;
        return 0;
    }

    private static int CmdDisableEAS(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd=41965;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData((byte) 4);
        snd_packet.WriteData((byte) 1);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int CmdLockEAS(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd =42221;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData((byte) 4);
        snd_packet.WriteData((byte) 1);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        pReader.m_syncLock.unlock();
        return iret;
    }

    private static int CmdEASCheck(L0_PICC_HANDLE h, SpecTagAccess tagAccess) {
        L0_ISO15693_PICC_HANDLE handle = (L0_ISO15693_PICC_HANDLE) h;
        DriverPC301 pReader = (DriverPC301) h.m_reader;
        SpecEASCheck pAccess = (SpecEASCheck) tagAccess;
        int iret = -1;
        pReader.m_syncLock.lock();
        BufferPack snd_packet = pReader.pbSnd;
        BufferPack recv_packet = pReader.pbRecv;
        snd_packet.flush();
        recv_packet.flush();
        char cmd =42477;
        snd_packet.WriteData(cmd);
        snd_packet.WriteData((byte) 4);
        snd_packet.WriteData((byte) 1);
        snd_packet.WriteData(handle.m_AddressMode);
        if (handle.m_AddressMode == 1) snd_packet.WriteData(handle.m_uid, 0, 8);
        iret = pReader.transceive(cmd, snd_packet, recv_packet, 500L, false);
        if (iret != 0) {
            pReader.m_syncLock.unlock();
            return iret;
        }
        if (recv_packet.readable_length() < 3) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        recv_packet.ReadData();
        recv_packet.ReadData();
        byte EAS_flag = recv_packet.ReadData();
        if (EAS_flag != 0 && recv_packet.readable_length() < 32L) {
            iret = -12;
            pReader.m_syncLock.unlock();
            return iret;
        }
        pAccess.m_respFlag = EAS_flag;
        iret = 0;
        pReader.m_syncLock.unlock();
        return iret;
    }
}
